﻿#define _CRT_SECURE_NO_WARNINGS 1

#if 1
#include <GL/glut.h>

// 方块的位置和大小
float blockX = 0.0f;
float blockY = 0.0f;
float blockSize = 0.5f;

// 鼠标按下时的位置
int mouseX = 0;
int mouseY = 0;

// 回调函数，处理键盘按下事件
void keyboardFunc(unsigned char key, int x, int y) {
	switch (key) {
	case 'w':
	case 'W':
		blockY += 0.1f; // 向上移动
		break;
	case 's':
	case 'S':
		blockY -= 0.1f; // 向下移动
		break;
	case 'a':
	case 'A':
		blockX -= 0.1f; // 向左移动
		break;
	case 'd':
	case 'D':
		blockX += 0.1f; // 向右移动
		break;
	}
	glutPostRedisplay(); // 重新绘制窗口
}

// 回调函数，处理鼠标事件
void mouseFunc(int button, int state, int x, int y) {
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
		mouseX = x;
		mouseY = y;
		glutMotionFunc([](int x, int y) {
			float dx = (x - mouseX) * 0.01f;
			float dy = (y - mouseY) * -0.01f; // 注意 y 坐标方向

			blockX += dx;
			blockY += dy;

			mouseX = x;
			mouseY = y;

			glutPostRedisplay(); // 重新绘制窗口
			});
	}
	else if (button == 3 && state == GLUT_UP) { // 滚轮向上滚动
		blockSize += 0.1f; // 放大方块
		glutPostRedisplay(); // 重新绘制窗口
	}
	else if (button == 4 && state == GLUT_UP) { // 滚轮向下滚动
		blockSize -= 0.1f; // 缩小方块
		if (blockSize < 0.1f) {
			blockSize = 0.1f; // 方块最小尺寸为 0.1
		}
		glutPostRedisplay(); // 重新绘制窗口
	}
}

// 渲染函数，绘制窗口内容
void renderFunc() {
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();

	// 绘制白色方块
	glColor3f(1.0f, 1.0f, 0.0f);
	glTranslatef(blockX, blockY, 0.0f);
	glScalef(blockSize, blockSize, 1.0f);
	glBegin(GL_QUADS);
	glVertex2f(-0.5f, -0.5f);
	glVertex2f(0.5f, -0.5f);
	glVertex2f(0.5f, 0.5f);
	glVertex2f(-0.5f, 0.5f);
	glEnd();

	glFlush();
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(600, 600);
	glutCreateWindow("Block Movement");

	glutDisplayFunc(renderFunc);
	glutKeyboardFunc(keyboardFunc); // 注册键盘回调函数
	glutMouseFunc(mouseFunc); // 注册鼠标回调函数

	glutMainLoop();
	return 0;
}
#endif 


#if 0
#include <Windows.h>
#include <gl/glut.h>

GLfloat AngleX, AngleY;

void init()
{
	AngleX = 45.0f;
	AngleY = 315.0f;
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_DITHER);
	glShadeModel(GL_SMOOTH);
}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glPushMatrix();
	{
		glRotatef(AngleX, 1.0f, 0.0f, 0.0f);
		glRotatef(AngleY, 0.0f, 1.0f, 0.0f);
		//绘制三维的正方体
		glBegin(GL_POLYGON);
		glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);
		glVertex3f(50.0f, 50.0f, 50.0f);

		glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);
		glVertex3f(50.0f, -50.0f, 50.0f);

		glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)0);
		glVertex3f(-50.0f, -50.0f, 50.0f);

		glColor3ub((GLubyte)255, (GLubyte)0, (GLubyte)255);
		glVertex3f(-50.0f, 50.0f, 50.0f);
		glEnd();

		glBegin(GL_POLYGON);
		glColor3f(0.0f, 1.0f, 1.0f);
		glVertex3f(50.0f, 50.0f, -50.0f);

		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f(50.0f, -50.0f, -50.0f);

		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(-50.0f, -50.0f, -50.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-50.0f, 50.0f, -50.0f);
		glEnd();

		glBegin(GL_POLYGON);
		glColor3d(0.0, 1.0, 1.0);
		glVertex3f(50.0f, 50.0f, -50.0f);

		glColor3d(1.0, 1.0, 1.0);
		glVertex3f(50.0f, 50.0f, 50.0f);

		glColor3d(1.0, 0.0, 1.0);
		glVertex3f(-50.0f, 50.0f, 50.0f);

		glColor3d(0.0, 0.0, 1.0);
		glVertex3f(-50.0f, 50.0f, -50.0f);
		glEnd();

		glBegin(GL_POLYGON);
		glColor3ub(0u, 255u, 0u);
		glVertex3f(50.0f, -50.0f, -50.0f);

		glColor3ub(255u, 255u, 0u);
		glVertex3f(50.0f, -50.0f, 50.0f);

		glColor3ub(255u, 0u, 0u);
		glVertex3f(-50.0f, -50.0f, 50.0f);

		glColor3ub(255u, 255u, 255u);
		glVertex3f(-50.0f, -50.0f, -50.0f);
		glEnd();

		glBegin(GL_POLYGON);
		glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)255);
		glVertex3f(50.0f, 50.0f, 50.0f);

		glColor3ub((GLubyte)0, (GLubyte)255, (GLubyte)255);
		glVertex3f(50.0f, 50.0f, -50.0f);

		glColor3ub((GLubyte)0, (GLubyte)255, (GLubyte)0);
		glVertex3f(50.0f, -50.0f, -50.0f);

		glColor3ub((GLubyte)255, (GLubyte)255, (GLubyte)0);
		glVertex3f(50.0f, -50.0f, 50.0f);
		glEnd();

		glBegin(GL_POLYGON);
		glColor3f(1.0f, 0.0f, 1.0f);
		glVertex3f(-50.0f, 50.0f, 50.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-50.0f, 50.0f, -50.0f);

		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(-50.0f, -50.0f, -50.0f);

		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-50.0f, -50.0f, 50.0f);
		glEnd();
	}
	glPopMatrix();
	glutSwapBuffers();
}

void reshape(int w, int h)
{
	GLfloat aspect = (GLfloat)w / (GLfloat)h;
	GLfloat nRange = 100.0f;

	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();


	if (w <= h)
	{
		glOrtho(-nRange, nRange, -nRange * aspect, nRange * aspect, -nRange, nRange);
	}
	else
	{
		glOrtho(-nRange, nRange, -nRange / aspect, nRange / aspect, -nRange, nRange);
	}
}

void key_board(int key, int x, int y)
{
	glMatrixMode(GL_MODELVIEW);
	glMatrixMode(GL_PROJECTION);

	if (key == GLUT_KEY_UP)//按“上”，放大图形	
		glScalef(1.5, 1.5, 1.5);

	if (key == GLUT_KEY_DOWN)//按“下”，缩小图形
		glScalef(0.5, 0.5, 0.5);

	if (key == GLUT_KEY_LEFT)//按“左”，旋转图形
		glRotated(10, 0, 1, 0);

	if (key == GLUT_KEY_RIGHT)//按“右”，右移图形
		glTranslated(10, 0, 0);



	glutPostRedisplay();
}

int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
	glutInitWindowSize(400, 400);
	glutCreateWindow("919106840202高宏艳-实验二");
	glutReshapeFunc(reshape);
	//glutKeyboardFunc(key_board);
	glutSpecialFunc(key_board);
	glutDisplayFunc(display);

	init();
	glutMainLoop();
	return 0;
}
#endif 

#if 0
#include <GL/glut.h>

int xd = 0, yd = 0;	//全局变量

void myDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.8, 0.5, 0.6);
	glPointSize(5);
	glBegin(GL_POLYGON);
	glVertex2i(10 + xd, 10 + yd);
	glVertex2i(20 + xd, 10 + yd);
	glVertex2i(20 + xd, 0 + yd);
	glVertex2i(10 + xd, 0 + yd);
	glEnd();
	glFlush();
}

void myKeyboard(unsigned char key, int x, int y)
{
	switch (key)
	{
	case 'w':yd++; break;
	case 's':yd--; break;
	case 'a':xd--; break;
	case 'd':xd++; break;
	}
	glutPostRedisplay();	//刷新显示
}

void init()
{
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-20, 30, -20, 30);	//可视的范围，类似鼠标滚轮的远近
}


int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(300, 100);
	glutInitWindowSize(600, 500);
	glutCreateWindow("key interaction");

	init();
	glutDisplayFunc(myDisplay);	//传递需要勾画的函数
	glutKeyboardFunc(myKeyboard);		//调用键盘函数
	glutMainLoop();
	return 0;
}
#endif 

#if 0
#include <GL/glut.h>
#include <stdlib.h>

GLfloat x = 0.0;
GLfloat y = 0.0;
GLfloat size = 50.0;
GLsizei wh = 500, ww = 500;

void drawSquare(GLint x, GLint y)
{
	y = wh - y;
	glBegin(GL_POLYGON);
	glVertex3f(x + size, y + size, 0);
	glVertex3f(x - size, y + size, 0);
	glVertex3f(x - size, y - size, 0);
	glVertex3f(x + size, y - size, 0);
	glEnd();
}

void myDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0, 1.0, 1.0);
	drawSquare(x, y);
	glutSwapBuffers();
	glutPostRedisplay();
}

void init()
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
}

void myReshape(GLint w, GLint h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, w, 0, h, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);

	ww = w;
	wh = h;
}

void myMouse(GLint button, GLint state, GLint wx, GLint wy)
{
	if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
		exit(0);
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
	{
		x = wx;
		y = wy;
	}
}

void main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(0, 0);
	glutCreateWindow("click to display square");
	init();
	glutDisplayFunc(myDisplay);
	glutReshapeFunc(myReshape);
	glutMouseFunc(myMouse);

	glutMainLoop();
}
#endif 